package com.six.sixbrush.selenium;

import lombok.Getter;
import lombok.Setter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;


@Component
public class Six {

    private final Map<String, String> tabMap = new HashMap<>();

    private final List<Bet> bets = new ArrayList<>();

    private final ChromeDriver chromeDriver = new ChromeDriver();

    public void openBrowser(String url) {
        chromeDriver.get(url);
    }

    /**
     * 验证码页面
     */
    public void findCode() {
        WebElement element = chromeDriver.findElement(By.id("code"));
        element.sendKeys("589356");
        WebElement btn = chromeDriver.findElement(By.id("btn"));
        btn.click();
    }

    public void nav() {
        WebElement element = chromeDriver.findElement(By.xpath("/html/body/table/tbody/tr[3]/td/table/tbody/tr[7]/td[1]"));
        element.click();
    }

    public void getBet() throws InterruptedException {
        goProxyTab();
        goMainFrame();
        List<WebElement> elements = chromeDriver.findElements(By.xpath("/html/body/table/tbody/tr/td[2]/table/tbody/tr"));
        bets.clear();
        for (int i = 2; i < elements.size(); i++) {
            List<WebElement> content = elements.get(i).findElements(By.tagName("font"));
            for (int j = 0; j < content.size(); j++) {
                if (j % 4 == 0) {
                    String text = content.get(j).getText();
                    if (isNumeric4(text)) {
                        Bet bet = new Bet();
                        bet.setCode(Integer.valueOf(text));
                        bet.setTotal(new BigDecimal(content.get(j + 2).getText()));
                        bet.setExpected(new BigDecimal(content.get(j + 3).getText()));
                        bets.add(bet);
                    }
                }
            }
        }
    }

    public void memberBetMain() {
        goMemberTab();
        goMemberMainFrame();
        for (int i = 1; i <= 49; i++) {
            String idString = "num_" + i;
            WebElement input = chromeDriver.findElement(By.id(idString));
            input.sendKeys(i+"");
        }
    }

    public Set<String> getAllWindow() {
        return chromeDriver.getWindowHandles();
    }

    public void setProxyTab() {
        String proxy = chromeDriver.getWindowHandle();
        tabMap.put("proxy", proxy);
    }

    public void setMemberTab() {
        String windowHandle = chromeDriver.getWindowHandle();
        tabMap.put("member", windowHandle);
//        Set<String> windowHandles = chromeDriver.getWindowHandles();
//        for (String windowHandle : windowHandles) {
//            if (!windowHandle.equals(tabMap.get("proxy"))) {
//                tabMap.put("member", windowHandle);
//            }
//        }
    }


    public void goProxyTab() {
        String proxy = tabMap.get("proxy");
        chromeDriver.switchTo().window(proxy);
    }

    public void goMemberTab() {
        String member = tabMap.get("member");
        chromeDriver.switchTo().window(member);
    }



    private static boolean isNumeric4(String str) {
        if (str == null) return false;
        for (char c : str.toCharArray ()) {
            if (!Character.isDigit(c)) return false;
        }
        return true;
    }

    @Getter
    @Setter
    private static class Bet {

        // 号码
        private Integer code;

        // 总额
        private BigDecimal total;

        // 预计
        private BigDecimal expected;
    }


    private void goMainFrame() {
        chromeDriver.switchTo().defaultContent();
        WebElement element = chromeDriver.findElement(By.id("login_frame"));
        chromeDriver.switchTo().frame(element);
        WebElement main = chromeDriver.findElement(By.id("main"));    // 第5层 第2个
        chromeDriver.switchTo().frame(main);
    }

    private void goMemberMainFrame() {
        chromeDriver.switchTo().defaultContent();
        WebElement element = chromeDriver.findElement(By.id("login_frame"));
        chromeDriver.switchTo().frame(element);
        WebElement main = chromeDriver.findElement(By.name("main"));
        chromeDriver.switchTo().frame(main);
    }


    public static void main(String[] args) throws InterruptedException {
        Six six = new Six();
        six.openBrowser("https://fv6.bba000.com/");
//        six.findCode();
//        Thread.sleep(15000);
//        six.nav();

        six.getBet();
    }
}
